/*******************************************************************************
 * Copyright 2011-2012 Dik Grapendaal
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package sh.grapendaal.tsuushin.message;

import java.util.Locale;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * Class representing a user on an IRC server.
 */
public class User {
	private static final int USERMASK_PARTS = 3;

	private String nickname;
	private String username;
	private String hostname;

	/**
	 * This class can only be instantiated from the factory method parse().
	 */
	protected User(String nickname) {
		this(nickname, null, null);
	}

	/**
	 * This class can only be instantiated from the factory method parse().
	 */
	protected User(String nickname, String username, String hostname) {
		this.nickname = nickname;
		this.username = username;
		this.hostname = hostname;
	}

	public String getNickname() {
		return nickname;
	}

	public String getUsername() {
		return username;
	}

	public String getHostname() {
		return hostname;
	}

	private boolean isNicknameOnly() {
		return username == null;
	}

	/**
	 * Parses the given user string to a User object. This can be a string in
	 * the form of nickname!username@hostname or just a nickname. The latter is
	 * possible when receiving server notices, the full user string will then be
	 * the hostname of the server sending the notice.
	 * 
	 * @param user
	 *            The user string to parse.
	 * @return The resulting User object containing the parsed information.
	 */
	public static User parse(String user) {
		User instance;
		int separator = user.indexOf('!');
		if (separator != -1) {
			String[] chunks = user.split("!|@", USERMASK_PARTS);
			instance = new User(chunks[0], chunks[1], chunks[2]);
		} else {
			instance = new User(user);
		}

		return instance;
	}

	@Override
	public boolean equals(Object object) {
		if (!(object instanceof User)) {
			return false;
		}
		if (this == object) {
			return true;
		}
		User rhs = (User) object;
		EqualsBuilder equals = new EqualsBuilder().append(this.nickname.toLowerCase(Locale.getDefault()),
				rhs.nickname.toLowerCase(Locale.getDefault()));
		if (!this.isNicknameOnly()) {
			equals.append(this.username.toLowerCase(Locale.getDefault()),
					rhs.username.toLowerCase(Locale.getDefault())).append(
					this.hostname.toLowerCase(Locale.getDefault()),
					rhs.hostname.toLowerCase(Locale.getDefault()));
		}
		return equals.isEquals();
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder().append(nickname);
		if (!this.isNicknameOnly()) {
			builder.append("!").append(username).append("@").append(hostname);
		}
		return builder.toString();
	}

	@Override
	public int hashCode() {
		HashCodeBuilder hashCode = new HashCodeBuilder()
				.append(this.nickname.toLowerCase(Locale.getDefault()));
		if (!this.isNicknameOnly()) {
			hashCode.append(this.username.toLowerCase(Locale.getDefault())).append(
					this.hostname.toLowerCase(Locale.getDefault()));
		}
		return hashCode.toHashCode();
	}
}
